package com.cloud.code.annotation;


import lombok.SneakyThrows;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Optional;

public class Test {

    @SneakyThrows
    public static void main(String[] args) {

        Class<?> aClass = Class.forName("com.cloud.code.annotation.User");

        Object o = aClass.newInstance();

        Annotation[] annotations = aClass.getAnnotations();
        Arrays.stream(annotations).forEach(annotation -> {
            boolean annotation1 = annotation.annotationType().isAnnotation();
            System.out.println(annotation);


        });

        MyAnnotation annotation = aClass.getAnnotation(MyAnnotation.class);
        Optional.ofNullable(annotation).ifPresent(myAnnotation -> {
            System.out.println(myAnnotation.name());
        });
        Method setName = aClass.getMethod("setName", String.class);
        setName.invoke(o,annotation.name());

        Method setAge = aClass.getMethod("setAge", int.class);
        setAge.invoke(o,annotation.age());

        Method setSex = aClass.getMethod("setSex", String.class);
        setSex.invoke(o,annotation.sex());
        System.out.println(o);


    }
}
